import Vue from 'vue'
import Router from 'vue-router'
import store from '../store/store'

//引入nprogress
import NProgress from 'nprogress'
import 'nprogress/nprogress.css' //这个样式必须引入
// 简单配置
NProgress.inc(0.2)
NProgress.configure({ easing: 'ease', speed: 500, showSpinner: false })

Vue.use(Router)

const router = new Router({
    mode: 'history',
    routes: [
        { path: '/', redirect: { name: 'home' } },
        { name: 'login', path: '/login', component: () => import('@/views/Login.vue') },
        {
            name: 'layout',
            path: '/layout',
            meta: { role: ['EDP', 'AD'] },
            redirect: { name: 'home' },
            component: () => import('@/views/Layout.vue'),
            children: [
                { name: 'home', path: '/home', meta: { role: ['EDP', 'AD'], title: '首页' }, component: () => import('@/views/Home.vue') },
                {
                    name: 'contentCreate',
                    path: '/content',
                    meta: { role: ['EDP'], title: '写交接' },
                    component: () => import('@/views/handover/Content.vue'),
                },
                {
                    name: 'contentEdit',
                    path: '/content/:id',
                    props: true,
                    meta: { role: ['EDP'], title: '修改交接' },
                    component: () => import('@/views/handover/Content.vue'),
                },
                {
                    name: 'handover',
                    path: '/handover',
                    meta: { role: ['EDP'], title: '交接查询' },
                    component: () => import('@/views/handover/Handover.vue'),
                },
                {
                    name: 'handoverExport',
                    path: '/handoverExport',
                    meta: { role: ['EDP'], title: '交接导出' },
                    component: () => import('@/views/handover/ExportHandover.vue'),
                },
                {
                    name: 'counterList',
                    path: '/counterList',
                    meta: { role: ['EDP', 'AD'], title: '专柜信息查询' },
                    component: () => import('@/views/counter/CounterList.vue'),
                },
                {
                    name: 'counterCreat',
                    path: '/counter',
                    meta: { role: ['EDP', 'AD'], title: '新增专柜信息' },
                    component: () => import('@/views/counter/Counter.vue'),
                },
                {
                    name: 'counterEdit',
                    path: '/counter/:id',
                    props: true,
                    meta: { role: ['EDP', 'AD'], title: '修改专柜信息' },
                    component: () => import('@/views/counter/Counter.vue'),
                },
                {
                    name: 'renewal',
                    path: '/renewal/:id/:name',
                    props: true,
                    meta: { role: ['EDP', 'AD'], title: '续租信息' },
                    component: () => import('@/views/counter/Renewal.vue'),
                },
                {
                    name: 'deviceList',
                    path: '/deviceList',
                    meta: { role: ['EDP'], title: '设备查询' },
                    component: () => import('@/views/device/DeviceList.vue'),
                },
                {
                    name: 'deviceCreate',
                    path: '/device',
                    meta: { role: ['EDP'], title: '新增设备' },
                    component: () => import('@/views/device/Device.vue'),
                },
                {
                    name: 'deviceEdit',
                    path: '/device/:id',
                    props: true,
                    meta: { role: ['EDP'], title: '修改设备信息' },
                    component: () => import('@/views/device/Device.vue'),
                },
                {
                    name: 'deviceRepairList',
                    path: '/deviceRepairList/:id',
                    props: true,
                    meta: { role: ['EDP'], title: '设备维修信息' },
                    component: () => import('@/views/device/DeviceRepairList.vue'),
                },
                {
                    name: 'repairCreate',
                    path: '/repair',
                    meta: { role: ['EDP'], title: '新增维修记录' },
                    component: () => import('@/views/device/Repair.vue'),
                },
                {
                    name: 'repairEdit',
                    path: '/repair/:id',
                    props: true,
                    meta: { role: ['EDP'], title: '修改维修记录' },
                    component: () => import('@/views/device/Repair.vue'),
                },
                {
                    name: 'partStoreList',
                    path: '/partStoreList',
                    meta: { role: ['EDP'], title: '配件库存' },
                    component: () => import('@/views/part/PartStoreList.vue'),
                },
                {
                    name: 'partPurchaseCreate',
                    path: '/partPurchase',
                    meta: { role: ['EDP'], title: '配件入库' },
                    component: () => import('@/views/part/PartPurchase.vue'),
                },
                {
                    name: 'partPurchase',
                    path: '/partPurchase/:id',
                    props: true,
                    meta: { role: ['EDP'], title: '配件入库' },
                    component: () => import('@/views/part/PartPurchase.vue'),
                },
                {
                    name: 'partPurchaseList',
                    path: '/partPurchaseList',
                    meta: { role: ['EDP'], title: '配件入库' },
                    component: () => import('@/views/part/PartPurchaseList.vue'),
                },
            ],
        },
        { name: '401', path: '/401', component: () => import('@/views/401.vue') },
        { name: '403', path: '/403', component: () => import('@/views/403.vue') },
        { name: '500', path: '/500', component: () => import('@/views/500.vue') },
        { name: '404', path: '*', component: () => import('@/views/404.vue') },
    ],
})

router.beforeEach((to, from, next) => {
    NProgress.start()
    if (
        to.name === 'login' ||
        !to.meta.role ||
        to.meta.role.includes(store.getters.user.role) ||
        to.meta.role.includes(sessionStorage.getItem('role'))
    ) {
        next()
    } else {
        Vue.prototype.$message.error('您还没有登录，或登录超时，请登录')
        next({ name: 'login' })
    }
})

router.afterEach(() => {
    NProgress.done()
})
export default router
